#pragma once

#include "transform3d.h"
#include "Lighting.h"
#include <QObject>


class QOpenGLContext;
class QOpenGLFunctions_3_3_Core;
class QOpenGLShaderProgram;
class QOpenGLVertexArrayObject;
class Camera;

struct aiScene;
struct aiMesh;


class MeshProg
        : public QObject
        , public Transform3D
{
    Q_OBJECT

public:
    explicit MeshProg(QObject *parent = nullptr);
    virtual ~MeshProg();

    bool initialize(QOpenGLContext* ctx, const aiMesh *mesh, const aiScene* scene, const QDir& dir);
    void paint(const Camera& camera, const QMatrix4x4& projection, const Light& light);

    const Material& material() const {return material_;}

public slots:
    void setMaterial(const Material& material);
    void reset();

private:
    unsigned int* initializeVertexIndices(size_t &numIndices);

private:
    QOpenGLContext*             ctx_;
    QOpenGLFunctions_3_3_Core*  glFunc_;
    QOpenGLShaderProgram*       prog_;
    QOpenGLVertexArrayObject*   vao_;
    QOpenGLBuffer*              vbo_;
    QOpenGLBuffer*              vboNormal_;
    QOpenGLBuffer*              vboTexture_;
    QOpenGLBuffer*              ebo_;
    QOpenGLTexture*             diffuseMap_;
    QOpenGLTexture*             specularMap_;

    const aiMesh*               mesh_;
    size_t                      numIndices_;
    unsigned int*               vertexIndices_;
    Material                    material_;
    int                         modelLoc_;
    int                         viewLoc_;
    int                         projectionLoc_;
    int                         viewPosLoc_;
};
